import axios from 'axios'
import qs from 'qs'
import NProgress from 'nprogress' // Progress 进度条
import 'nprogress/nprogress.css' // Progress 进度条样式
import { Message } from 'element-ui';
import routes from "../routes";


// NProgress.start() - 显示进度条
// NProgress.set(0.4) - 设置百分比
// NProgress.inc() - 稍微增加
// NProgress.done() - 完成进度(进度条消失)
// NProgress.inc(0.2)
// NProgress.configure({ easing: 'ease', speed: 500, showSpinner: false })

// http请求拦截器
axios.interceptors.request.use(config => {
    NProgress.start()
    return config
}, error => {
    return Promise.reject(error)
})

const message_conf = {
    message: '服务器无响应',
    type: 'error',
    duration: 2000
}
// http响应拦截器
axios.interceptors.response.use(data => { // 响应成功关闭loading
    NProgress.done()
    // console.log(data)
    return data
}, error => {
    // Message(message_conf)
    NProgress.done()
    // console.log(error)
    console.log(error.response.data.code)
    if (error.response.data.code === 3) {
        sessionStorage.clear()
        // routes.push('/login')
        // setTimeout(() => {
        //     location.reload()
        // }, 2000)
        let path = window.location.href.split('#')[1]
        if (path !== '/login') {
            sessionStorage.clear()
            setTimeout(() => {
                location.reload()
            }, 1500)
        }
        return Promise.reject(error)
    } else {
        return Promise.reject(error)
    }
})

// let url
//
// if (process.env.NODE_ENV === 'development') {
//     url = '/api'
// } else {
//     url = '/api'
// }

// axios.defaults.headers = {
//     'Content-Type': 'application/x-www-form-urlencoded'
// }

// 超时时间
axios.defaults.timeout = 10000

// export function get(url, params){
//     return new Promise((resolve, reject) =>{
//         axios.get(url, {
//             params: params
//         }).then(res => {
//             resolve(res.data);
//         }).catch(err =>{
//             reject(err.data)
//         })
//     });}


// export function post(url, params) {
//     return new Promise((resolve, reject) => {
//         axios.post(url, QS.stringify(params))
//             .then(res => {
//                 resolve(res.data);
//             })
//             .catch(err =>{
//                 reject(err.data)
//             })
//     });
// }

let baseUrl;
if (process.env.NODE_ENV === 'development') {
    baseUrl = '/api'
} else {
    baseUrl = 'https://mobile.laiba.shop'
}

const get = function (url, params) {
    params = params || {};
    return new Promise(((resolve, reject) => {
        axios.get(baseUrl + url, {
            params: params
        }).then(res => {
            resolve(res.data);
        }).catch(err => {
            reject(err)
        })
    }))
}

const post = function (url, params) {
    params = params || {};
    return new Promise(((resolve, reject) => {
        axios.post(baseUrl + url, params)
            .then(res => {
                resolve(res.data);
            }).catch(err => {
                reject(err)
        })
    }))
}

export { get, post }
